﻿/////////////////////////////////////////////////////////////////////////
//                                                                     //
//    MTGProject - A Magic The Gathering Networked Environment         //
//                                                                     //
//  Copyright (c) 2008-2009 by The MTGProject Team (CSC230 & MAT227)   //
//                                                                     //
// This file is part of MTGProject.                                    //
//                                                                     //
// MTGProject is free software; you can redistribute it and/or         //
// modify it under the terms of the GNU Lesser General Public          //
// License as published by the Free Software Foundation; either        //
// version 3 of the License, or (at your option) any later version.    //
//                                                                     //
// This library is distributed in the hope that it will be useful,     //
// but WITHOUT ANY WARRANTY; without even the implied warranty of      //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       //
// GNU Lesser General Public License for more details.                 //
//                                                                     //
// You should have received a copy of the GNU Lesser General Public    //
// License along with this library; if not, write to the Free Software //
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA           //
// 02111-1307, USA, or contact the author(s):                          //
//                                                                     //
// Ruben Acuna <flyingfowlsoftware@earthlink.net>                      //
// Chris Watson <the5thdimension@hotmail.com>                          //
//                                                                     //
/////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;

namespace MTGProject
{
    public class CardPool : ISubject
    {
        #region variables

        private LinkedList<ICard> cards;
        private Random generator;
        private Zone zone;
        LinkedList<IObserver> observers;

        #endregion

        #region properties

        public LinkedList<ICard> Cards
        {
            get { return cards; }
            set { cards = value; }
        }

        #endregion

        #region consturctor

        public CardPool(Zone zoneType)
        {
            cards = new LinkedList<ICard>();
            observers = new LinkedList<IObserver>();
            generator = new Random();
            this.zone = zoneType;
        }

        #endregion

        #region methods

        /// <summary>
        /// Removes and then returns first card with name. Returns null if card not found.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ICard GetCardByName(string name)
        {
            name = name.ToLower();

            foreach (ICard card in Cards)
                if (card.Name.ToLower().Equals(name))
                {
                    return card;
                }

            return null;
        }

        public bool HasCard(ICard card)
        {
            foreach (ICard myCard in Cards)
                if (myCard == card)
                    return true;

            return false;
        }

        public bool HasCardByName(string name)
        {
            name = name.ToLower();

            foreach (ICard myCard in Cards)
                if(myCard.Name.ToLower().Equals(name))
                    return true;

            return false;
        }

        public int countCard(string name)
        {
            int count = 0;

            name = name.ToLower();

            foreach (ICard myCard in Cards)
                if (myCard.Name.ToLower().Equals(name))
                    count++;

            return count;
        }

        public void Shuffle()
        {
            ICard[] cardArray = new ICard[cards.Count];
            cards.CopyTo(cardArray, 0);

            for(int x = 0; x < cardArray.Length; x++)
            {
                int first = generator.Next(0, cards.Count);
                int second = generator.Next(0, cards.Count);
                ICard temp = cardArray[first];

                cardArray[first] = cardArray[second];
                cardArray[second] = temp;
            }

            cards = new LinkedList<ICard>();

            foreach (ICard card in cardArray)
                AddCard(card);
        }

        public ICard Draw()
        {
            ICard card = Cards.First.Value;

            RemoveCard(card);

            return card;
        }

        public CardPool DrawCardPool(int number)
        {
            CardPool drawn = new CardPool(Zone.OTHER);

            for (int x = 0; x < number; x++)
                drawn.AddCard(Draw());

            return drawn;
        }

        public void AddCard(ICard card)
        {
            Cards.AddFirst(card);

            notifyObservers(Observation.CARD_ADDED, card);
        }

        public void AddCardPool(CardPool pool)
        {
            foreach (ICard card in pool.Cards)
                AddCard(card);
        }

        public void RemoveCard(ICard card)
        {
            Cards.Remove(card);

            notifyObservers(Observation.CARD_REMOVED, card);
        }

        public void RemoveCardPool(CardPool pool)
        {
            foreach (ICard card in pool.Cards)
                RemoveCard(card);
        }

        public CardPool clone()
        {
            CardPool clonedCardPool = new CardPool(zone);

            foreach(ICard card in Cards)
                clonedCardPool.AddCard(card);

            //TODO: should we clone observers?

            return clonedCardPool;
        }

        public override String ToString()
        {
            return "CardPool." + zone;
        }

        #endregion

        #region methods for ISubject

        public void RegisterObserver(IObserver observer)
        {
            //if (observer is ICard)
            //    System.Console.WriteLine("CardPool: Added observer " + ((ICard)observer).Name + ".");

            observers.AddLast(observer);
        }

        public void RemoveObserver(IObserver observer)
        {
            //if (observer is ICard)
            //    System.Console.WriteLine("CardPool: Removed observer " + ((ICard)observer).Name + ".");

            observers.Remove(observer);
        }

        private void notifyObservers(Observation message, object cause)
        {
            foreach (IObserver observer in observers)
                observer.Update(message, this, cause);
        }

        #endregion
    }
}
